<?php

namespace App\Http\Controllers\Api;

use App\Models\Trend;
use App\Repositories\Api\MemberMatchRepository;
use App\Repositories\Api\MemberRepository;
use App\Repositories\Api\PipeiMappingRepository;
use App\Repositories\Api\TrendRepository;

class PipeiController extends MiniAppController
{
    private $memberRep = NULL,
        $ppmRepo = NULL,
        $matchRep = NULL;

    public function __construct(
        MemberRepository       $memberRep,
        PipeiMappingRepository $ppmRepo,
        MemberMatchRepository  $matchRep
    )
    {
        parent::__construct();

        $this->memberRep = $memberRep;
        $this->ppmRepo = $ppmRepo;
        $this->matchRep = $matchRep;
    }

    public function index()
    {
//        获取最新一条动态
        return retSuc([
            'items' => $this->ppmRepo->getPipeiMapping(UID, 5),
            'trend' => (new TrendRepository())->getLastest()
        ]);
    }

    /**
     * 分配会员
     * @Author   thomashtao               bosslen
     * @DateTime 2023-11-16T16:20:09+0800
     * @param string $value [description]
     * @return   [type]                           [description]
     */
    public function recommend()
    {
        $params = request()->post();
        $params['ip'] = request()->ip();

        $member = $this->memberRep->getSimpleMember(UID, 3);
        $filter['m.gender'] = $member['gender'] == "M" ? "F" : "M";

        if ($this->memberRep->checkBaseProfile()) { //基础资料完成
            $extra_condition['in']['id'] = $this->matchRep->recommend($params);
        } else {//基础资料未完成
        }
        $extra_condition['field'] = 2;
//        dd($extra_condition);
        $res = $this->memberRep->getPages($filter, 5, 1, [], $extra_condition ?? []);

        return retSuc([
            'data' => $res['items'] ?? [],
            'trend' => (new TrendRepository())->getLastest()
        ]);
    }

    public function check()
    {
        $res = $this->memberRep->checkBaseProfile();
        $flag1 = !$res;

        if ($res) {
            $userIds = $this->matchRep->getUserIds(UID);
            $flag1 = !empty($userIds);
        }

        return retSuc(['data' => $flag1]);
    }

    /**
     * 今日推荐
     */
    public function today()
    {
        $filter['today'] = 1;

        return retSuc([
            'items' => $this->matchRep->getLists($filter, 1, 5),
            'trend' => (new TrendRepository())->getLastest()
        ]);
    }

    public function update_visit_time()
    {
        $df_uid = request()->post("df_uid");
        return $this->matchRep->updateVisitTime(UID, $df_uid);
    }

    /**
     * 最近三天的
     * @return
     */
    public function history()
    {
        $res = $this->matchRep->getHistoryList(UID);
        return retSuc(['items' => $res]);
    }

}